package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkCalcFormulaGroup;
import com.ysd.lis.mapper.WkCalcFormulaGroupMapper;
import com.ysd.lis.request.WkCalcFormulaRequest;
import com.ysd.lis.service.WkCalcFormulaGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-04-30
 */
@Service
public class WkCalcFormulaGroupServiceImpl extends ServiceImpl<WkCalcFormulaGroupMapper, WkCalcFormulaGroup> implements WkCalcFormulaGroupService {

    @Autowired
    WkCalcFormulaGroupMapper mapper;


    @Override
    public Result pageList(WkCalcFormulaRequest.PageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkCalcFormulaGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkCalcFormulaGroup::getDelFlag, 0);
            if (StringUtils.isNotBlank(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(
                        w -> w.like(WkCalcFormulaGroup::getCode, pageParam.getSearchValue())
                                .or()
                                .like(WkCalcFormulaGroup::getGroupName, pageParam.getSearchValue())
                );
            }
            lambdaQueryWrapper.orderByAsc(WkCalcFormulaGroup::getCreateTime);
            List<WkCalcFormulaGroup> wkCalcFormulas = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(wkCalcFormulas);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result add(WkCalcFormulaGroup params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            if (StringUtils.isNotBlank(params.getStartDateStr())) {
                params.setStartDate(strToDate.apply(params.getStartDateStr()));
            }
            if (StringUtils.isNotBlank(params.getEndDateStr())) {
                params.setEndDate(strToDate.apply(params.getEndDateStr()));
            }
            int insert = mapper.insert(params);
            if (insert < 0) {
                result = Result.fail(400, "添加失败！", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "添加失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result updateDit(WkCalcFormulaGroup params) {
        if (StringUtils.isNotBlank(params.getStartDateStr())) {
            params.setStartDate(strToDate.apply(params.getStartDateStr()));
        }
        if (StringUtils.isNotBlank(params.getEndDateStr())) {
            params.setEndDate(strToDate.apply(params.getEndDateStr()));
        }
        boolean b = this.updateById(params);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail(400, "编辑失败！", "");
        }
    }

    @Override
    public Result delete(WkCalcFormulaGroup deleteParams) {
        Result result;
        try {
            int i = mapper.deleteById(deleteParams.getId());
            if (i > 0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", e.getMessage());
        }
        return result;
    }

    private static final Function<String, Date> strToDate = x -> {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(x);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    };
}
